#include <iostream.h>
#include <fstream.h>
#include <math.h>
#define pi 3.14159

int xc=0, yc=0;       // current coordinates.       
ofstream write;

int larger (int x, int y)
{
	if (x>y)
		return x;
	else return y;
}

float angle(int x, int y, float r)
{
	float th;
	th=asin(y/r);
	if (x<0&&y>0)
		th+=pi;
	if (x<0&&y<0)
		th-=pi;
	if (x<0)
		th=-th;
	th*=180/pi;
	return th;
}

void line()
{
	int x,y,l;
	cout<<"enter the next movement :\n";
	cin>>x>>y;
	l=larger(x,y);
	for(int i=1,xt=0,yt=0; i<=l;i++)
	{
		if (i*x/l>=xt+1)
		{
			xc++;
			xt++;
			write <<"+1";
		}
		else if (i*x/l<=xt-1)
		{
			xc--;
			xt--;
			write <<"-1";
		}
		else
			write <<"0";
		write <<"\t";
		if (i*y/l>=yt+1)
		{
			yc++;
			yt++;
			write <<"+1";
		}
		else if (i*y/l<=yt-1)
		{
			yc--;
			yt--;
			write <<"-1";
		}
		else
			write <<"0";
		write<<"\n";
	}
}

void circle()
{
	int x,y;
	int th;
	float tht,r;
	cout<<"enter the coordinates of the center of the circle,\nrelative to the current coordinates :\n";
	cin>>x>>y;
	cout<<"enter the angle of the arc in degrees (anticlockwise is positive) :";
	cin >>th;
	r=sqrt(x*x+y*y);
	tht=angle (-x,-y,r);
	for(int i=1; i<=th;i+=01)
	{
		if (r*cos((tht+i)*pi/180)>x+1)
		{
			x++;
			xc++;
			write <<"+1";
		}
		else if (r*cos((tht+i)*pi/180)<x-1)
		{
			x--;
			xc--;
			write <<"-1";
		}
		else
			write <<"0";
		write <<"\t";
		if (r*sin((tht+i)*pi/180)>y+1)
		{
			y++;
			yc++;
			write <<"+1";
		}
		else if (r*sin((tht+i)*pi/180)<y-1)
		{
			y--;
			yc--;
			write <<"-1";
		}
		else
			write <<"0";
		write <<"\n";
		cout<<xc<<"\t"<<yc<<"\n";
	}
}

void jump()
{
	int x,y;
	cout<<"current coordinates : "<<xc<<" , "<<yc<<endl;
	cout<<"enter the displacement from the current coordinates :\n";
	cin>>x>>y;
	xc+=x;
	yc+=y;
}

void main()
{
	char op='c';
	write.open("data.txt");
	while (op=='l'||op=='c'||op=='j')
	{
		cout<<"current coordinates : "<<xc<<" , "<<yc<<endl<<endl;
		cout<<"select an option : \n\t\t";
		cout<<"l : draw a line\n\t\t";
		cout<<"c : draw a circular arc\n\t\t";
		cout<<"j : jump to another point\n\t\t";
		cout<<"press any other key to exit\n\t\t";
		cin>>op;
		if (op=='l')
			line();
		if (op=='c')
			circle();
		if (op=='j')
			jump();
	}
	cout<<"current coordinates : "<<xc<<" , "<<yc<<endl;
	cout<<"\nthe text file containing the CNC data has been generated.\nThank You for using this program.\n";
	write.close();
}